using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.Skinner;
using System.IO;
using System.Collections.Generic;
using Ent.Web;
using Ent.Voice.Show;
public partial class Controls_Common_SkinSelector : Ent.Web.WebControl.UserControl
{
    const string SkinSelector_Page = "SkinSelector_Page";
    private string _width = "100%";
    private int maxOnScreen = 12;
    private int maxInRow = 3;
    private Skin[] skins = null;
    private int start = 0;
    bool _doPaginate = false;
    public new string Width
    {
        get { return _width; }
        set { _width = value; }
    }
    public Controls_Common_SkinSelector()
    {
        this.PreRender += new EventHandler(SkinSelector_PreRender);
    }
    void SkinSelector_PreRender(object sender, EventArgs e)
    {
        if (Session[SkinSelector_Page] == null)
        {
            Session[SkinSelector_Page] = 1;
            _doPaginate = true;
        }
        if (_doPaginate)
            PopulateSkins(skins, start);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        skins = PageSkinner.GetSkins(base.Server.MapPath("Data/Skin.xml"));
        try
        {
            divJavascript.InnerHtml = Javascript.ReadAllText(Server.MapPath("Javascripts/ScreenSelector.js"), null);
        }
        catch (Exception exc)
        { }
        if (_width == "")
            tabContainer.Width = "100%";
        else
            tabContainer.Width = _width;
        if (!IsPostBack)
        {
            hfSkinSelected.Value = "default";
        }
        tblSkins.CssClass = "tabSecondLevel";
        tblSkins.Height = Unit.Percentage(100);
        PopuplateControls();
    }
    public override void Init()
    {
        foreach (string key in Values.Keys)
        {
            //string noIdkey = key.Replace(ID + "_", "");
            switch (key)
            {
                case EventProperty.SkinId:
                    hfSkinSelected.Value = Values[key].ToString() ;
                    break;
                default:
                    break;
            }
        }
    }
    public override void Dispose()
    {
        Session[SkinSelector_Page] = null;
    }
    private void PopulateSkins(Skin[] skins, int start)
    {
        int maxInColumn = maxOnScreen / maxInRow;
        int end = start + maxOnScreen;
        if (skins.Length < end)
        {
            end = skins.Length;
            lnkNext.Visible = false;
        }
        else
            lnkNext.Visible = true;
        if (start <= 0)
        {
            start = 0;
            lnkPrevious.Visible = false;
        }
        else
            lnkPrevious.Visible = true;
        Dictionary<string, string> images = new Dictionary<string, string>();
        TableRow tr = new TableRow();
        
        for (int i= start; i< end;i++)
        {
            Skin skin = (Skin)skins.GetValue(i);
            
            if (skin.Id != "")
            {
                TableCell tc = GetCell();
                tc.Style.Add(HtmlTextWriterStyle.BorderColor, "#2060a7");
                tc.Style.Add("border-top-style", "solid");
                tc.Style.Add("border-left-style", "solid");
                tc.Style.Add("border-top-width", "1px");
                tc.Style.Add("border-left-width", "1px");
                tc.HorizontalAlign = HorizontalAlign.Left;
                tc.Width = Unit.Percentage(100 / (10 * maxInRow));
                RadioButton rb = new RadioButton();
                if (hfSkinSelected.Value == "" && skin.Id == "100")
                {
                    rb.Checked = true;
                    hfSkinSelected.Value = skin.Id;
                }
                rb.ID = "rb_" + skin.Id;
                rb.Attributes.Add("onclick", "document.getElementById('" + hfSkinSelected.ClientID + "').value='" + skin.Id + "'");
                rb.GroupName = "skin";
                tc.Controls.Add(rb);
                tr.Cells.Add(tc);

                tc = GetCell();
                tc.Style.Add(HtmlTextWriterStyle.BorderColor, "#2060a7");
                tc.Style.Add("border-top-style", "solid");
                tc.Style.Add("border-right-style", "solid");
                tc.Style.Add("border-top-width", "1px");
                tc.Style.Add("border-right-width", "1px");
                tc.HorizontalAlign = HorizontalAlign.Left;
                tc.Width = Unit.Percentage(900 / (10 * maxInRow));
                tc.Text = "<font color=\"#2060a7\">" + skin.Name + "</font>";
                tr.Cells.Add(tc);
                
                if (File.Exists(Server.MapPath(skin.ThumbImagePath)))
                    images.Add(skin.Id,skin.ThumbImagePath);
                else
                    images.Add(skin.Id, "Images/skin/thumbnopreview.jpg");
            }
            int rem = 0;
            Math.DivRem(i, maxInRow, out rem);
            if (rem == (maxInRow - 1) || i == skins.Length - 1)
            {
                tblSkins.Rows.Add(tr);
                tr = new TableRow();
                foreach (string imageid in images.Keys)
                {
                    TableCell tc = GetCell();
                    tc.Style.Add(HtmlTextWriterStyle.BorderColor, "#2060a7");
                    tc.Style.Add("border-left-style", "solid");
                    tc.Style.Add("border-right-style", "solid");
                    tc.Style.Add("border-left-width", "1px");
                    tc.Style.Add("border-right-width", "1px");
                    tc.Style.Add("border-bottom-style", "solid");
                    tc.Style.Add("border-bottom-width", "1px");
                    tc.Width = Unit.Percentage(100 / maxInRow);
                    tc.Height = Unit.Percentage(100 / maxInColumn);
                    tc.Style.Add(HtmlTextWriterStyle.PaddingLeft, "25px");
                    tc.HorizontalAlign = HorizontalAlign.Left;
                    tc.ColumnSpan = 2;
                    tc.Text = "<img id=\"img_" + imageid + "\" src=\"" + images[imageid] + "\"/>";
                    tr.Cells.Add(tc);
                    tblSkins.Rows.Add(tr);
                }
                images = new Dictionary<string, string>();
                tr = new TableRow();
            }
        }
    }
    private TableCell GetCell()
    {
        TableCell tc = new TableCell();
        tc.VerticalAlign = VerticalAlign.Top;
        tc.HorizontalAlign = HorizontalAlign.Left;
        return tc;
    }
    public override Dictionary<string, object> GetValues()
    {
        Dictionary<string, object> retValue = new Dictionary<string, object>();
        retValue.Add(EventProperty.SkinId, hfSkinSelected.Value);
        return retValue;
    }
    private int Try_ParseInt(object o)
    {
        try
        {
            return Convert.ToInt32(o);
        }
        catch (Exception exc)
        {
            return -1;
        }
    }
    protected void lnkPrevious_Click(object sender, EventArgs e)
    {
        int page = Try_ParseInt(Session[SkinSelector_Page]);
        page = page == -1 ? 0 : page;
        start = maxOnScreen * (page - 2);
        if (skins.Length < start)
            return;
        Session[SkinSelector_Page] = page - 1;
        _doPaginate = true;
    }
    protected void lnkNext_Click(object sender, EventArgs e)
    {
        int page = Try_ParseInt(Session[SkinSelector_Page]);
        page = page == -1 ? 0 : page;
        start = maxOnScreen * page;
        if (skins.Length < start)
            return;
        Session[SkinSelector_Page] = page + 1;
        _doPaginate = true;
    }
    public void PopuplateControls() { }
    public override void SetEvents() { }
    public override void AddJavascript() { }
}
